package com.example.demo.admin.gecco;/**
 * Created by kete on 2017-11-07.
 */

import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.pipeline.PipelineFactory;
import com.geccocrawler.gecco.spider.SpiderBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 作者：kete
 * 创建时间：2017-11-07 17:29
 * 功能描述：
 * 版本：
 */
@Component
public class GeccoPipelineFactory implements ApplicationContextAware, PipelineFactory {

    private ApplicationContext applicationContext;

    @Override
    public Pipeline<? extends SpiderBean> getPipeline(String name) {
        try {
            Object ex = this.applicationContext.getBean(name);
            if(ex instanceof Pipeline) {
                return (Pipeline)ex;
            }
        } catch (NoSuchBeanDefinitionException var3) {
            System.out.println("no such pipeline : " + name);
        }

        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;

    }
}
